package com.woody.aggregate.application.service.saas.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.woody.aggregate.IdDTO;
import com.woody.aggregate.application.exception.BusinessException;
import com.woody.aggregate.application.service.saas.SaasShopIndustryService;
import com.woody.aggregate.enums.BusinessExceptionEnum;
import com.woody.aggregate.infrastructure.saas.entity.SaasShopIndustry;
import com.woody.aggregate.infrastructure.saas.mapper.SaasShopIndustryMapper;
import com.woody.aggregate.saas.dto.*;
import com.woody.aggregate.saas.vo.PageQuerySaasIndustryVO;
import com.woody.aggregate.saas.vo.SaasShopIndustryVO;
import com.woody.common.web.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class SaasShopIndustryServiceImpl extends ServiceImpl<SaasShopIndustryMapper, SaasShopIndustry> implements SaasShopIndustryService {

    /**
     * 一级行业编码
     */
    private final static Long LEVEL = 100L;

    @Override
    public Page<PageQuerySaasIndustryVO> pageQuery(PageQuerySaasIndustryDTO pageQuerySaasIndustryDTO) {
        IPage<SaasShopIndustry> page = ChainWrappers.lambdaQueryChain(getBaseMapper())
                .likeRight(StringUtils.isNotBlank(pageQuerySaasIndustryDTO.getName()), SaasShopIndustry::getName, pageQuerySaasIndustryDTO.getName())
                .eq(Objects.nonNull(pageQuerySaasIndustryDTO.getState()), SaasShopIndustry::getState, pageQuerySaasIndustryDTO.getState())
                .ge(Objects.nonNull(pageQuerySaasIndustryDTO.getCreateTimeBgn()), SaasShopIndustry::getCreateTime, pageQuerySaasIndustryDTO.getCreateTimeBgn())
                .le(Objects.nonNull(pageQuerySaasIndustryDTO.getCreateTimeEnd()), SaasShopIndustry::getCreateTime, pageQuerySaasIndustryDTO.getCreateTimeEnd())
                .eq(SaasShopIndustry::getLevel,1)
                .page(pageQuerySaasIndustryDTO.genPage());

        List<PageQuerySaasIndustryVO> resultList = new ArrayList<>();
        page.getRecords().forEach(e -> {
            PageQuerySaasIndustryVO pageQuerySaasIndustryVO = new PageQuerySaasIndustryVO();
            BeanUtils.copyProperties(e, pageQuerySaasIndustryVO);
            resultList.add(pageQuerySaasIndustryVO);
        });

        Page<PageQuerySaasIndustryVO> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(resultList);
        return resultPage;
    }

    @Override
    public SaasShopIndustryVO getById(Long id) {
        SaasShopIndustry saasShopIndustry = this.getBaseMapper().selectById(id);
        if (Objects.isNull(saasShopIndustry)) {
            return null;
        }
        SaasShopIndustryVO saasShopIndustryVO = JacksonUtil.reGroup(saasShopIndustry, SaasShopIndustryVO.class);
        return saasShopIndustryVO;
    }

    @Override
    public Map<String, Object> add(AddSaasShopIndustryDTO addSaasShopIndustryDTO) {
        if (addSaasShopIndustryDTO.getLevel() > 3) {
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR.getCode(), "层级不能大于三");
        }

//        if (this.nameExist(addPlatformIndustryDTO.getName(),null)) {
//            throw new BaseException(UserExceptionEnum.PARAM_ERROR, "行业已存在:" + addPlatformIndustryDTO.getName());
//        }

        Long code = this.buildMaxCode(addSaasShopIndustryDTO.getParentCode(), addSaasShopIndustryDTO.getLevel());

        SaasShopIndustry saasShopIndustryEntity = new SaasShopIndustry();
        BeanUtils.copyProperties(addSaasShopIndustryDTO, saasShopIndustryEntity);
        saasShopIndustryEntity.setCode(code);
        this.save(saasShopIndustryEntity);
        Long id = saasShopIndustryEntity.getId();

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("code", code);
        return map;
    }

    /**
     * 构建code
     *
     * @param parentCode
     * @param level
     * @return
     */
    private Long buildMaxCode(Long parentCode, Integer level) {
        Long code = this.getMaxCode(parentCode);
        if (Objects.nonNull(code)) {
            code = code + 1;
            return code;
        }

        if (Objects.nonNull(level) && level.equals(1) && Objects.nonNull(parentCode) && parentCode.equals(0)) {
            return LEVEL;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(parentCode).append("").append(LEVEL);
        return Long.valueOf(sb.toString());
    }

    /**
     * 获得当期最大编码
     *
     * @param parentCode
     * @return
     */
    private Long getMaxCode(Long parentCode) {
        Long maxCode = this.getBaseMapper().getMaxCode(parentCode);
        return maxCode;
    }

    @Override
    public Map<String, Object> edit(UpdateSaasShopIndustryDTO updateSaasShopIndustryDTO) {
        SaasShopIndustry saasShopIndustryEntity = JacksonUtil.reGroup(updateSaasShopIndustryDTO, SaasShopIndustry.class);
        this.updateById(saasShopIndustryEntity);

        SaasShopIndustry entity = this.getBaseMapper().selectById(updateSaasShopIndustryDTO.getId());
        Long id = saasShopIndustryEntity.getId();
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("code", entity.getCode());
        return map;
    }

    @Override
    public Long delete(IdDTO idDTO) {
        this.getBaseMapper().deleteById(idDTO.getId());
        return idDTO.getId();
    }

    @Override
    public List<SaasShopIndustryVO> firstLevelIndustryList(QueryFirstLevelIndustryDTO queryFirstLevelIndustryDTO) {
        List<SaasShopIndustry> list = ChainWrappers.lambdaQueryChain(getBaseMapper())
                .eq(Objects.nonNull(queryFirstLevelIndustryDTO.getState()), SaasShopIndustry::getState, queryFirstLevelIndustryDTO.getState())
                .eq(Objects.nonNull(queryFirstLevelIndustryDTO.getShowVal()), SaasShopIndustry::getShowVal, queryFirstLevelIndustryDTO.getShowVal())
                .eq(SaasShopIndustry::getLevel, 1).list();
        if(CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        List<SaasShopIndustryVO> saasShopIndustryVOS = JacksonUtil.reGroupArray(list, SaasShopIndustryVO[].class);
        return saasShopIndustryVOS;
    }

    @Override
    public List<SaasShopIndustryVO> likeRightByCode(CodeQueryDTO codeQueryDTO) {
        List<SaasShopIndustry> list = ChainWrappers.lambdaQueryChain(getBaseMapper()).likeRight(SaasShopIndustry::getCode, codeQueryDTO.getCode()).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<SaasShopIndustryVO> saasShopIndustryVOS = JacksonUtil.reGroupArray(list, SaasShopIndustryVO[].class);
        return saasShopIndustryVOS;
    }
}
